"""
@author: jiangpj
@email: pj.jiang@hzgosun.com
@time: 2019/5/22 11:32

前台用户管理的增删改查自动化测试用例编写

"""
import unittest
from time import sleep
from selenium.webdriver.common.keys import Keys
from src.common.decorators import log4gs, mkdir_classname
from src.common.functions import save_screenshot
from src.common.settings import TAG
from src.common.vue.select import DropDownSelect, OperateSelect
from src.pages.userViewListPage import UserViewList


@log4gs
@mkdir_classname(TAG)
class UserViewListTest(unittest.TestCase):
    """前台用户管理功能测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始前台用户管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = UserViewList('/UserManage/UserViewList')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束前台用户管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))
        sleep(2)

    def test_1_page(self):
        """前台用户翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        if int(self.page.turnPage.total_page) >= 3:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')
            sleep(2)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(3)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '3')
        else:
            self.log.error("当前模块最大未到3页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    # @unittest.skip
    def test_2_add_user(self):
        """前台用户新增功能验证"""
        name = "自动前台用户"
        sleep(1)
        self.page.table.add_btn.click()
        sleep(1)
        self.page.addPerson.idname.send_keys(name)
        sleep(1)
        self.page.addPerson.gen.click()
        DropDownSelect(self.page.addPerson.gen, "综治")
        sleep(1)
        DropDownSelect(self.page.addPerson.id_, "区级")
        sleep(1)
        self.page.addPerson.region.click()
        # sleep(1)
        self.page.addPerson.region_selector.select("河北省-保定市-清苑区")
        self.page.addPerson.region.click()  # 收起区域选择器
        sleep(1)
        DropDownSelect(self.page.addPerson.jue, "江测试")
        self.page.addPerson.name.click()
        sleep(1)
        self.page.addPerson.name.send_keys("王尚端")
        sleep(1)
        DropDownSelect(self.page.addPerson.state, "启用")
        sleep(1)
        self.page.addPerson.confirm.click()
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(name)
        self.page.searchTools.number.clear()
        sleep(1)
        try:
            self.assertTrue(result, "通过账号名称:{}".format(name))
            self.log.info("用例{}前台用户新增{}执行成功".format(self.id(), name))
        except Exception:
            self.log.error("用例{}前台用户新增{}执行失败,".format(self.id(), name), exc_info=True)

    # @unittest.skip
    def test_3_1_modiy(self):
        """前台用户修改功能验证"""
        name = "自动前台用户"
        name2 = "测试前台"
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(name)
        OperateSelect(result, "查看")
        sleep(1)  # 等待弹窗完全出来
        self.page.modifyPerson.name.clear()
        sleep(1)
        self.page.modifyPerson.name.send_keys(name2)
        sleep(1)
        self.page.modifyPerson.confirm.click()
        self.page.searchTools.number.clear()
        sleep(1)
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        result2 = self.page.table.get_row(name2)
        self.page.searchTools.number.clear()
        sleep(1)
        try:
            self.assertTrue(result2)
            self.log.info("前台用户管理修改成功")
        except Exception:
            self.log.error("前台用户管理修改失败,", exc_info=True)

    def test_3_2_stop(self):
        """前台用户停用功能验证"""
        name = "自动前台用户"
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.number.clear()
        result = self.page.table.get_row(name)
        OperateSelect(result, "停用")
        stop_rwsult = self.page.table.get_row("停用")
        try:
            self.assertTrue(stop_rwsult, msg="前台用户停用成功")
            self.log.info("停用前台用户停用执行成功")
        except Exception:
            self.log.error("停用前台用户停用执行失败", exc_info=True)
        sleep(2)
        OperateSelect(result, "启用")
        sleep(1)
        start_up_result = self.page.table.get_row("启用")
        try:
            self.assertTrue(start_up_result, msg="前台用户启用成功")
            self.log.info("启用前台用户启用执行成功")
        except Exception:
            self.log.error("启用前台用户启用执行失败", exc_info=True)
        sleep(1)
        OperateSelect(result, "重置密码")

    def test_4_remove(self):
        """前台用户停用删除"""
        name = "自动前台用户"
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.number.clear()
        result = self.page.table.get_row(name)
        OperateSelect(result, "停用")
        sleep(1)
        OperateSelect(result, "删除")
        sleep(1)
        self.page.rmPerson.confirm.click()
        stop_rwsult = self.page.table.get_row(name)
        try:
            self.assertFalse(stop_rwsult)
            self.log.info("停用前台用户{}删除执行成功".format(name))
        except Exception:
            self.log.error("停用前台用户{}删除执行失败".format(name), exc_info=True)

    def test_5_search(self):
        """账号名称检索"""
        sleep(1)
        self.page.searchTools.number.send_keys("二七")
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row("二七综治办")
        self.page.searchTools.number.clear()
        sleep(1)
        try:
            self.assertTrue(result, "通过账号名称:二七，查询到了二七综治办")
            self.log.info(f"用例{self.id()}执行成功")
        except Exception:
            self.log.error(f"用例{self.id()}执行失败,", exc_info=True)


if __name__ == '__main__':
    unittest.main()
